import { isWechat } from '@/config/h5Utils';
import { setCookie,getCookie,removeCookie } from '@/plugins/cookie';

export const state = {
    //用户数据
    userInfo: {
		token:false,
		bid:1
	},
	chatmsgnoread:0,
	siteInfo:{
		bid:1,
	},
	locAtion: uni.getStorageSync('location') ? uni.getStorageSync('location') : {locale:0},
	extConfig:uni.getStorageSync('ext_data') ? uni.getStorageSync('ext_data') : {
			bid:1,shopid:45
	}
};
export const mutations = {
	  set_extConfig(state, data) {
		  console.log('set_extConfig',data)
		if (data) {
			uni.setStorageSync('ext_data', data);
			state.extConfig = data;
		}
	  },
	  
	setChatmsgnoread(state, data) {
		if (data) {
			state.chatmsgnoread = data;
		}
	},
	  
    //储存用户信息
    setUserInfo(state, data) {
        if (data) {
            state.userInfo =  Object.assign({}, state.userInfo,data);
            // #ifdef H5
      // if(isWechat()){
        // 微信浏览器使用cookie存储---ios微信浏览器使用此方式可以拿到cookie
        var val = encodeURIComponent(JSON.stringify(state.userInfo)) //这里对保存的数据进行编码
        setCookie('userInfo',val,15)
      // }else{
      //  window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo));
      // }
            // #endif
            // #ifndef H5
            uni.setStorageSync('userInfo', state.userInfo);
            // #endif
        }
    },
	  //定位
	  setLocation(state, data) {
		if (data) {
			uni.setStorageSync('location', data);
			state.location = data;
		}
	  },
	
    //储存用户信息
    setSiteInfo(state, data) {
        if (data) {
            state.siteInfo =  Object.assign({}, state.siteInfo,data);
            // #ifdef H5
      // if(isWechat()){
        // 微信浏览器使用cookie存储---ios微信浏览器使用此方式可以拿到cookie
        var val = encodeURIComponent(JSON.stringify(state.siteInfo)) //这里对保存的数据进行编码
        setCookie('siteInfo',val,15)
      // }else{
      //  window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo));
      // }
            // #endif
            // #ifndef H5
			// #endif
            uni.setStorageSync('siteInfo', state.siteInfo);
            
        }
    },
    // 退出APP
    emptyUserInfo(state) {
        state.userInfo = {};
        // #ifdef H5
        // if(isWechat()){
          removeCookie('userInfo')
        // }else{
        //  window.sessionStorage.removeItem("userInfo");
        // }
        // #endif
        // #ifndef H5
        uni.removeStorageSync("userInfo");
        // #endif
    },
};
export const actions = {
	// 获取用户信息
	getUserInfo({commit},data) {
		return new Promise((resolve, reject) => {
			console.log('getuserinfo')
            uni.$u.http.post('api/member.Member/info',{
				member_id:data.member_id
			},{custom: {load:false}}).then((res) => {
				console.log(res)
				commit("setUserInfo",{...res});
				resolve(res);
            }).catch((err) =>{
            	resolve(false);reject(err)
            })
		})
	},
	getsiteInfo({commit}, data) {
		return new Promise((resolve, reject) => {
            uni.$u.http.post('api/setting.Setting/initsite',
				{bid:data.bid,},{custom: {load:false}}).then((res) => {
				commit("setSiteInfo",{...res});
				resolve(res);
            }).catch((err) =>{
            	resolve(false);reject(err)
            })
		})
	},

};
